05.Working with providers
What are providers?
A PowerShell provider, or PSProvider, is an adapter. It’s designed to take some kind of data storage, such as Windows Registry, Active Directory, or even the local filesystem, and make it look like a disk drive.
You can see a list of installed PowerShell providers right within the shell:
Get-PSProvider
You use a provider to create a PSDrive. A PSDrive uses a single provider to connect to data storage.
Get-PSDrive
The PSProvider adapts the data store, and the PSDrive makes it accessible.
Get-Command -Noun *item*
Because it’s probably the one you’re most familiar with, we’ll start with the filesystem—the FileSystem
PSProvider.
Understanding how the filesystem is organized
The filesystem is organized around two main types of objects—folders and files. Folders are also a kind of container, capable of containing both files and other folders. Files aren’t a type of container; they’re more of an endpoint object.
PowerShell doesn’t use the terms file and folder. Instead, it refers to these objects by the more generic term item.
Set-Item
------------------ Example 1: Create an alias ------------------
Set-Item -Path alias:np -Value "c:\windows\notepad.exe"
---- Example 2: Change the value of an environment variable ----
Set-Item -Path env:UserRole -Value "Administrator"
------------ Example 3: Modify your prompt function ------------
Set-Item -Path function:prompt -Value {'PS '+ (Get-Date -Format t) + " " + (Get-Location) + '> '}
------- Example 4: Set options for your prompt function -------
Set-Item -Path function:prompt -Options "AllScope,ReadOnly"
Some PSProviders don't support item properties. For example, the Environment
PSProvider is what's used to make the ENV:
drive available in PowerShell. This drive provides access to the environment variables, but as the following example shows, they don't have item properties:
Get-ItemProperty -Path Env:\PSModulePath
Navigating the filesystem
Using wildcards and literal paths
-Path
: Wildcards are permitted.-LiteralPath
: No characters are interpreted as wildcards.
Note that -LiteralPath
isn't positional; if you plan to use it, you have to type -LiteralPath
.
Working with other providers
PowerShell includes the following aliases for Write-Output
:
- All platforms: - `echo`
- Windows: - `write`
Write-Output
----- Example 1: Get objects and write them to the console -----
$P = Get-Process
Write-Output $P
----------- Example 2: Pass output to another cmdlet -----------
Write-Output "test output" | Get-Member
---------- Example 3: Suppress enumeration in output ----------
Write-Output 1,2,3 | Measure-Object
Count : 3
...
Write-Output 1,2,3 -NoEnumerate | Measure-Object
Count : 1
...
Windows Commands | Description | Linux + macOS |
---|---|---|
echo TEXT | Print TEXT to the terminal | |
echo TEXT > TARGET | Print TEXT to a file named TARGET | |
echo TEXT >> TARGET | Append TEXT to TARGET |
Lab
NOTE For this lab, you need any computer running PowerShell v7.1 or later.
Complete the following tasks from a PowerShell prompt:
- Create a new directory called
Labs
. - Create a zero-length file named
/Labs/Test.txt
(useNew-Item
). - Is it possible to use
Set-Item
to change the contents of/Labs/Test.txt
to-TESTING
? Or do you get an error? If you get an error, why? TheFileSystem
provider doesn’t support this action. You useWrite-Output
to add text content from PowerShell into a file:Write-Output "First line text." > TARGET
Write-Output "Second line text content" >> TARGET
- Using the Environment provider, display the value of the system environment variable
PATH
. - Use help to determine what the differences are between the
-Filter
,-Include
, and-Exclude
parameters ofGet-ChildItem
.-Include
and-Exclude
must be used with–Recurse
or if you’re querying a container.-Filter
uses the PSProvider’s filter capability, which not all providers support. For example, you could useDIR –filter
in the filesystem.